package com.xiaotu.spider.aiman.pageprocessor;

import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

/**
 * @类名 StarAiManPageProcessor
 * @日期 2017年11月29日
 * @作者 王艳龙
 * @功能 艾漫明星粉丝数据爬取
 */
@Component("StarAiManPageProcessor")
@Scope("prototype")
public class StarAiManPageProcessor extends APageProcessor{

	@Override
	protected Request getStartRequest(JSONObject target) {
		return new Request("http://"+PropertiesUtil.getProperty("aiman.serv.url")+"/?star=s&keyword="
                + KeywordEncode.encode(target.getString("name")));
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		if (url.contains("keyword"))
			return Constants.PageDataType.SEARCH_LIST;
		else if (url.contains("objectid"))
			return Constants.PageDataType.TV_RECORD;
		return Constants.PageDataType.UNKNOW;
	}
	
	@Override
	public void process(Page page) {
		Map<String, Object> tvInfoMap = page.getRequest().getExtras();
        String dataType = this.getPageDataType(page);

        switch (dataType)
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, tvInfoMap);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.tvRecordProcess(page, tvInfoMap);
                break;
            default:
                break;
        }
		
	}

	 private void searchListProcess(Page page,final Map<String, Object> tvInfoMap){
		 	String jsonStr = page.getRawText();
		 	jsonStr=jsonStr.replace("\\", "");
		 	jsonStr = jsonStr.substring(jsonStr.indexOf("{\"data\""));
			jsonStr = jsonStr.substring(0, jsonStr.lastIndexOf("\"}"));
			JSONObject C_json = JSONObject.fromObject(jsonStr);
			
			if (!jsonStr.contains("data") && !jsonStr.contains("artistTab") && !jsonStr.contains("listOfArtist"))
				throw new SpiderException(
						SpiderExceptionCode.DataInvalideException);
			String objectIds = C_json.getJSONObject("data").getString("artistTab");
			JSONObject objectIdJson = JSONObject.fromObject(objectIds);
			JSONArray datas = objectIdJson.getJSONArray("listOfArtist");
			for (int i = 0; i < datas.size(); i++) {
				JSONObject comment = datas.getJSONObject(i);
				String objectId =comment.getString("objectId");
				String objectName =comment.getString("objectName");
				if(objectName.equals(tvInfoMap.get("name"))){
					PageUtils.addTargetRequest(page,
							"http://"+PropertiesUtil.getProperty("aiman.serv.url")+"/?star=s&objectid="+objectId,
							tvInfoMap, true);
				}
			}
	 }
	 
	 private void tvRecordProcess(Page page, Map<String, Object> tvInfoMap){
		 //获取数据转Model
		 AiManModelUtils.setAiManModel(page, tvInfoMap,"star");
			
	 }
	
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.STAR_AIMAN;
	}
}
